package com.skeeey.aim.service.impl;


import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.skeeey.aim.model.Role;
import com.skeeey.aim.model.User;
import com.skeeey.aim.model.UserRole;
import com.skeeey.aim.service.UserService;
import com.skeeey.aim.service.dao.DaoException;
import com.skeeey.aim.service.dao.RoleDao;
import com.skeeey.aim.service.dao.UserDao;
import com.skeeey.aim.service.dao.UserRoleDao;

public class UserServiceImpl implements UserService {
	private Logger logger = Logger.getLogger(UserServiceImpl.class);
	
	private UserDao userDao;
	private RoleDao roleDao;
	private UserRoleDao userRoleDao;
	
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public void setRoleDao(RoleDao roleDao) {
		this.roleDao = roleDao;
	}

	public void setUserRoleDao(UserRoleDao userRoleDao) {
		this.userRoleDao = userRoleDao;
	}

	@Override
	public User getUserByAccount(String name) {
		return userDao.getUserByAccount(name);
	}

	@Override
	public boolean saveUser(User user, String roleName) {
		boolean r = false;
		try {
			userDao.save(user);
			
			Role role = roleDao.findRoleBy(roleName);
			if(role != null) {
				UserRole userRole = new UserRole();
				userRole.setRole(role);
				userRole.setUser(user);
				userRoleDao.save(userRole);
				r = true;
			}
		} catch (DaoException e) {
			logger.warn(e.getMessage(), e);
		}
		return r;
	}

	@Override
	public List<User> getUsers() {
		List<User> users =  new ArrayList<User>();
		for(User user : userDao.list()) {
			if(!"root".equals(user.getAccount())) {
				UserRole userRole = userRoleDao.findUserRoleListBy(user).get(0);
				user.setRoleName(userRole.getRole().getName());
				users.add(user);
			}
		}
		return users;
	}

	@Override
	public void deletUserBy(String userId) {
		try {
			User user = userDao.load(userId);
			List<UserRole> userRoles = userRoleDao.findUserRoleListBy(user);
			for(UserRole userRole : userRoles) {
				userRoleDao.delete(userRole);
			}
			userDao.delete(user);
		} catch (DaoException e) {
			logger.error(e.getMessage(), e);
		}
	}

	@Override
	public void updateUserBy(String userId, String userName,
			String userTelePhone, String userEmail, String roleName) {
		try {
			User user = userDao.load(userId);
			user.setName(userName);
			user.setTelephone(userTelePhone);
			user.setEmail(userEmail);
			userDao.update(user);
			
			UserRole userRole = userRoleDao.findUserRoleListBy(user).get(0);
			if(!userRole.getRole().getName().equals(roleName)) {
				Role role = roleDao.findRoleBy(roleName);
				userRole.setRole(role);
				userRoleDao.update(userRole);
			}
			
		} catch (DaoException e) {
			logger.error(e.getMessage(), e);
		}
	}

	@Override
	public List<User> findUsersBy(String userAccount) {
		if(userAccount == null || userAccount.length() == 0) {
			return getUsers();
		}
		
		List<User> users = new ArrayList<User>();
		User u = userDao.getUserByAccount(userAccount);
		if(u != null && !"root".equals(u.getAccount())) {
			String roleName = userRoleDao.findUserRoleListBy(u).get(0).getRole().getName();
			u.setRoleName(roleName);
			users.add(u);
		}
		return users;
	}

	@Override
	public void updatePasswordBy(String userId, String newPassword) {
		User user = userDao.get(userId);
		if(user != null) {
			user.setPassword(newPassword);
			try {
				userDao.update(user);
			} catch (DaoException e) {
				logger.error(e.getMessage(), e);
			}
		}
	}

	@Override
	public Role findUserRoleBy(User user) {
		Role role = null;
		if(user != null) {
			role = userRoleDao.findUserRoleListBy(user).get(0).getRole();
		}
		return role;
	}
}
